package com.paas.form.core.model;

import com.paas.common.util.StringUtil;
import com.paas.form.core.element.field.Config;
import com.paas.form.core.element.field.FieLdsModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@EqualsAndHashCode(callSuper = true)
@Data
public class ElFormItem extends ElementUI {

    private String _label;

    private String _prop;

    private FieLdsModel field;

    public ElFormItem(FieLdsModel fieLdsModel, int indentNum) {
        this.field = fieLdsModel;
        Config config = fieLdsModel.getConfig();
        this._label = config.getLabel();
        this._prop = fieLdsModel.getVModel();
        this.indentNum = indentNum;
    }

    public void getElHtml(StringBuilder str) {

    }

    @Override
    public void getStartTagElHtml(StringBuilder str) {
        String codeTemplate = String.join("", Collections.nCopies(this.indentNum, "\t")) + "<el-form-item${[label]}${[prop]}";
        if(this.field.getConfig().getNoShow() != null &&this.field.getConfig().getNoShow().equals(true)){
            codeTemplate = codeTemplate +" v-show=\"false\" ";
        }
        codeTemplate = codeTemplate +" >";
        //
        Map<String, Object> params = new HashMap<>();
        params.put("label", StringUtil.isNotEmpty(this._label) ? " label=\"" + this._label + "\"" : null);
        params.put("prop", StringUtil.isNotEmpty(this._prop) ? " prop=\"" + this._prop + "\"" : null);
        //
        ExpressionParser parser = new SpelExpressionParser();
        TemplateParserContext parserContext = new TemplateParserContext("${", "}");
        str.append(parser.parseExpression(codeTemplate, parserContext).getValue(params, String.class));
        str.append("\n");
    }

    @Override
    public void getEndTagElHtml(StringBuilder str) {
        String codeTemplate = String.join("", Collections.nCopies(this.indentNum, "\t")) + "</el-form-item>";
        str.append(codeTemplate);
        str.append("\n");
    }
}